//
//  SearchResultViewController.m
//  F50
//
//  Created by zack on 15/5/21.
//  Copyright (c) 2015年 ZACK. All rights reserved.
//

#import "SearchResultViewController.h"
#import "RoothomeTableViewCell.h"
#import "ProjectInfoViewController.h"
@interface SearchResultViewController ()<UITableViewDataSource,UITableViewDelegate>
{
    UITableView *_tableView;
    NSMutableArray *dataArr;
    UIImageView *isCare;// 关注 或者取消关注的那个图片
}

@end

@implementation SearchResultViewController

- (void)createBackground
{
    UIView *nav_bg = [[UIView alloc] initWithFrame:CGRectMake(0, 0, WIDTH, 64)];
    nav_bg.backgroundColor = RGB(59, 107, 178);
    [self.view addSubview:nav_bg];
    
    
    UILabel *nav_title = [[UILabel alloc] initWithFrame:CGRectMake(WIDTH/2 - 80, 25, 160, 30)];
    nav_title.text = self.titleStr;
    nav_title.textAlignment = NSTextAlignmentCenter;
    nav_title.font = [UIFont boldSystemFontOfSize:20];
    nav_title.textColor = [UIColor whiteColor];
    nav_title.backgroundColor = [UIColor clearColor];
    [nav_bg addSubview:nav_title];
    
    UIButton *backBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    backBtn.frame = CGRectMake(20, 32, 15, 18);
    [backBtn setBackgroundImage:[UIImage imageNamed:@"back"] forState:UIControlStateNormal];
    [backBtn addTarget:self action:@selector(back) forControlEvents:UIControlEventTouchUpInside];
    [nav_bg addSubview:backBtn];
    
}

-(void)viewWillAppear:(BOOL)animated{
    self.tabBarController.tabBar.hidden = YES;
    dataArr = [[NSMutableArray alloc]init];
    
    NSDictionary * date = [[NSDictionary  alloc]initWithObjectsAndKeys:[USER_DEFAULT objectForKey:@"token"],@"token",self.searchStr,@"keywords",self.industryStr,@"industry",self.stageStr,@"stage",self.stateStr,@"state",nil];
    NSDictionary *pram = [[NSDictionary alloc]initWithObjectsAndKeys:@"search",@"class",@"search",@"func",date,@"param", nil];
    
    [[AFCustomObject shareAFCustomObject] doRequestActionWithParams:pram success:^(NSDictionary *result) {
      
        
        NSArray *array = [result objectForKey:@"result"];
        
        for (int i = 0; i < array.count; i++) {
            NSDictionary *dict=array[i];
            RoothomeDataModle *modle=[[RoothomeDataModle alloc] init];
            modle.title = [dict objectForKey:@"title"];
            modle.imageUrl = [dict objectForKey:@"thumb"];
            modle.projectID = [dict objectForKey:@"id"];
            modle.releasetime = [dict objectForKey:@"releasetime"];
            modle.info = [dict objectForKey:@"description"];
            modle.stage = [dict objectForKey:@"stage"];
            modle.state = [dict objectForKey:@"state"];
            modle.relations = [dict objectForKey:@"relations"];
            modle.deals = [dict objectForKey:@"deals"];
            modle.amount = [dict objectForKey:@"amount"];
            modle.isrelation = [dict objectForKey:@"isrelation"];
            
            [dataArr addObject:modle];
        }
        [_tableView reloadData];
        
        
        
    } fail:^{
        
    }];

    
    
    
    
    

}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    [self createBackground];

    _tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 64, WIDTH, HEIGHT - 64 -49) style:UITableViewStylePlain];
    
    _tableView.dataSource = self;
    _tableView.delegate = self;
    _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    //    _tableView.scrollEnabled = NO; //设置tableview 不能滚动
    _tableView.backgroundColor = [UIColor clearColor];
    [self.view addSubview:_tableView];
    
    
    isCare = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, 90, 105)];
    isCare.center = CGPointMake(WIDTH/2, HEIGHT/2);
    isCare.alpha = 0;
    [self.view addSubview:isCare];
    
}

#pragma mark ----- uitableviewdelegate
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    
    return dataArr.count;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    static NSString *cellIndentifier = @"cell";
    RoothomeTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIndentifier];
    if (cell == nil) {
        cell = [[RoothomeTableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIndentifier];
    }
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    cell.backgroundColor = [UIColor clearColor];
    
    RoothomeDataModle *modle = dataArr[indexPath.row];
    [cell setCellLableText:modle];
    cell.blocks = ^(int isrelation){
        if (isrelation) {
            isCare.image = [UIImage imageWithImageName:@"care@2x"];
            isCare.alpha = 1;
            
            [UIView animateWithDuration:1.0
                                  delay:0.0
                                options:UIViewAnimationOptionCurveEaseInOut //设置动画类型
                             animations:^{
                                 //开始动画
                                 isCare.alpha = 0;
                             }
                             completion:^(BOOL finished){
                                 // 动画结束时的处理
                             }];
            
            
        }else{
            NSLog(@"weiguanzhu");
            
            isCare.image = [UIImage imageWithImageName:@"delete@2x"];
            isCare.alpha = 1;
            
            [UIView animateWithDuration:1.0
                                  delay:0.0
                                options:UIViewAnimationOptionCurveEaseInOut //设置动画类型
                             animations:^{
                                 //开始动画
                                 isCare.alpha = 0;
                             }
                             completion:^(BOOL finished){
                                 // 动画结束时的处理
                             }];
            
        }
        
    };

    
    
    return cell;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    if ( [[USER_DEFAULT objectForKey:@"iscertificated"] intValue] == 1) {
        UIAlertView *alertView = [[UIAlertView alloc]initWithTitle:@"提示" message:@"您还没有进行认证" delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil, nil];
        [alertView show];
        
        return;
    }
    
    ProjectInfoViewController *pInfo = [[ProjectInfoViewController alloc]init];
    pInfo.modle = dataArr[indexPath.row];
    [self.navigationController pushViewController:pInfo animated:YES];
    
    
    
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 120;
}




- (void)back{
    [self.navigationController popViewControllerAnimated:YES];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
